package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色管理
 *
 * @author choujin
 * @create 2019/12/16 11:46
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    /**
     * 角色分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            return new Result(true, "角色查询成功", roleService.findPage(queryPageBean));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色查询失败");
        }
    }

    /**
     * 新增角色
     *
     * @param permissionIds
     * @param role
     * @return
     */
    @RequestMapping("/add")
    public Result add(Integer[] permissionIds, @RequestBody Role role) {
        //去掉空格
        role.setKeyword(role.getKeyword().trim());
        role.setName(role.getName().trim());
        if (role.getName() == null || "".equals(role.getName())) {
            return new Result(false, "角色名不能为空");
        }
        if (role.getKeyword() == null || "".equals(role.getKeyword())) {
            return new Result(false, "角色编码不能为空");
        }
        try {
            roleService.add(permissionIds, role);
            return new Result(true, "新增角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增角色失败");
        }
    }

    /**
     * 编辑角色
     *
     * @param permissionIds
     * @param role
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds, @RequestBody Role role) {
        //去掉空格
        role.setKeyword(role.getKeyword().trim());
        role.setName(role.getName().trim());
        if (role.getName() == null || "".equals(role.getName())) {
            return new Result(false, "角色名不能为空");
        }
        if (role.getKeyword() == null || "".equals(role.getKeyword())) {
            return new Result(false, "角色编码不能为空");
        }
        try {
            roleService.edit(permissionIds, role);
            return new Result(true, "编辑角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑角色失败");
        }
    }

    /**
     * 根据rid查询角色具有的权限
     *
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionIds")
    public Result findPermissionIds(Integer id) {
        try {
            Integer[] permissionIds = roleService.findPermissionIds(id);
            return new Result(true, "角色权限查询成功", permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色权限失败");
        }
    }

    /**
     * 根据角色id查询角色
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, "查询角色成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色失败");
        }
    }

    /**
     * 查询要删除的角色是否关联其他表
     *
     * @param id
     * @return
     */
    @RequestMapping("/queryIsRelevance")
    public Result queryIsRelevance(Integer id) {
        Boolean flag=true;
        try {
            flag = roleService.queryIsRelevance(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(flag,"");
    }

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> list=roleService.findAll();
            return new Result(true,"查询角色成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色失败");
        }
    }

    /**
     * 删除角色
     * @param id
     * @param flag
     * @return
     */
    @RequestMapping("/del")
    public Result del(Integer id,String flag){
        if ("".equals(flag)||flag.length()==0){
            return new Result(false,"请正确提交数据");
        }
        if (id==0){
            return new Result(false,"请正确提交数据");
        }
        try {
            roleService.del(id,flag);
            return new Result(true,"删除角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除角色失败");
        }
    }

}
